<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item
          :label="
            isActive == 5
              ? '交货日期'
              : isActive == 2 || isActive == 6
              ? '加急交货日期'
              : '下单日期'
          "
          style="width: 350px"
        >
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="生产单编号" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="shipmentType">
          <el-input
            v-model.trim="queryParams.shipmentType"
            style="width: 180px"
            placeholder="请输入产品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-select
            v-model="queryParams.workshopName"
            clearable
            style="width: 180px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in workshopOptions"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="机台"
          v-if="isActive == 8 || isActive == 9"
          prop="machineId"
        >
          <el-select
            filterable
            clearable
            @change="getTasksApi"
            v-model="queryParams.machineId"
          >
            <el-option
              v-for="item in workbranchList"
              :label="item.machineName"
              :key="item.pid"
              :value="item.pid"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Search"
          :disabled="loading"
          :color="isActive == 1 ? 'orange' : ''"
          @click="handleQueryOrderSummary"
          >生产单汇总</el-button
        >
        <el-button
          type="primary"
          icon="Search"
          :disabled="loading"
          :color="isActive == 2 ? 'orange' : ''"
          @click="handleQueryShortageGoods"
          >欠货排查</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 3 ? 'orange' : ''"
          icon="Search"
          @click="handleQueryPrint"
          >待印刷</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 4 ? 'orange' : ''"
          icon="Search"
          @click="handleQueryBinding"
          >待装订</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 5 ? 'orange' : ''"
          icon="Search"
          @click="handleQueryInvoice"
          >已发货</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 6 ? 'orange' : ''"
          icon="Search"
          @click="handleQueryUnfulfilledOrders"
          >未履约订单</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 7 ? 'orange' : ''"
          icon="Search"
          @click="handleQueryQuestionOrder"
          >问题订单</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 8 ? 'orange' : ''"
          icon="Search"
          @click="handlePrintTasks"
          >印刷已排产</el-button
        >
        <el-button
          type="primary"
          :disabled="loading"
          :color="isActive == 9 ? 'orange' : ''"
          icon="Search"
          @click="handleBindTasks"
          >装订已排产</el-button
        >

        <!-- <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        > -->
      </el-col>
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        height="65vh"
        :data="dataList"
        v-if="dataList.length"
        :span-method="spanMethod"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="90"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="数据状态"
          min-width="90"
          align="center"
          :show-overflow-tooltip="true"
          prop="isSuspend"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <el-tag
              :type="row.isSuspend == 1 ? 'error' : 'success'"
              effect="dark"
              round
            >
              {{ row.isSuspend == 1 ? "暂停" : "正常" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="130"
          label="生产单编号"
          prop="produceNo"
        />

        <el-table-column
          min-width="200"
          align="center"
          label="产品名称"
          prop="shipmentType"
        />
        <el-table-column
          align="center"
          label="车间"
          min-width="90"
          prop="workshopName"
        />
        <el-table-column
          align="center"
          label="下单日期"
          min-width="115"
          prop="submitDate"
        >
          <template v-slot="{ row }">
            {{ row.submitDate && row.submitDate.split("T")[0] }}
          </template>
        </el-table-column>

        <el-table-column
          v-if="isActive == 2 || isActive == 6"
          align="center"
          min-width="125"
          label="加急交货日期"
          prop="urgentDate"
        >
          <template v-slot="{ row }">
            <span style="color: red">
              {{ parseTime(row.urgentDate) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="isActive == 2 || isActive == 6"
          align="center"
          min-width="125"
          label="加急交货数量"
          prop="urgentNum"
        >
          <template v-slot="{ row }">
            <span style="color: red">
              {{ row.urgentNum }}
            </span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          min-width="115"
          label="交货日期"
          prop="deliverDate"
        >
          <template v-slot="{ row }">
            {{ row.deliverDate && row.deliverDate.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="品种"
          prop="breedType"
          min-width="100"
        />

        <el-table-column align="center" label="组成" prop="composition" />
        <el-table-column
          align="center"
          label="印张"
          prop="sheet"
          min-width="60"
        />

        <!-- <el-table-column
                    align="center"
                    label="版材尺寸"
                    prop="sheetSize"
                  >
                    <template #default="scope">
                      <dict-tag
                        :options="ems_machine_info_size"
                        :value="scope.row.sheetSize"
                      />
                    </template> 
                  </el-table-column>-->

        <el-table-column
          align="center"
          label="订单数量"
          prop="orderCount"
          min-width="90"
        />

        <el-table-column
          align="center"
          label="用纸令数"
          prop="usePaperNum"
          min-width="90"
        />
        <el-table-column
          v-if="isActive == 5"
          align="center"
          label="发货用纸令数"
          prop="lingCount"
          min-width="90"
        />
        <el-table-column
          align="center"
          label="制版状态"
          prop="zbDate"
          min-width="90"
        >
          <template #default="scope">
            {{ scope.row.zbDate }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="印刷状态"
          prop="ysDate"
          min-width="90"
        >
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.ysDate == '已完成' || scope.row.ysDate == '印刷中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '1')"
                text
              >
                {{ scope.row.ysDate }}
              </el-button>
            </span>
            <span style="color: red" v-else-if="scope.row.ysDate == '--'">
              {{ scope.row.ysDate }}
            </span>
            <span v-else>
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '2')"
                text
              >
                {{ scope.row.ysDate }}
              </el-button>
            </span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="折页状态"
          prop="zyDate"
          min-width="90"
        >
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.zyDate == '已完成' || scope.row.zyDate == '折页中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '2')"
                text
              >
                {{ scope.row.zyDate }}
              </el-button>
            </span>
            <span v-else-if="scope.row.zyDate == '已排产'">
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '4')"
                text
              >
                {{ scope.row.zyDate }}
              </el-button>
            </span>
            <span style="color: red" v-else>
              {{ scope.row.zyDate }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="装订状态"
          prop="zdDate"
          min-width="90"
        >
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.zdDate == '已完成' || scope.row.zdDate == '装订中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '5')"
                text
              >
                {{ scope.row.zdDate }}
              </el-button>
            </span>
            <span v-else-if="scope.row.zdDate == '已排产'">
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '3')"
                text
              >
                {{ scope.row.zdDate }}
              </el-button>
            </span>
            <span style="color: red" v-else> {{ scope.row.zdDate }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="发货数量"
          prop="fhDate"
          min-width="90"
        >
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewCount(scope.row, '3')"
              text
            >
              {{ scope.row.fhDate }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="未入库用纸令数"
          prop="notInPaperNum"
          min-width="150"
        />
      </el-table>
      <!-- 印刷已排产 
      装订已排产 -->
      <el-table
        v-else
        v-loading="loading"
        border
        height="65vh"
        :data="taskList"
        :summary-method="getSummariesTask"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          align="center"
          label="车间"
          min-width="90"
          prop="workshopName"
        />
        <el-table-column
          align="center"
          label="机台名称"
          min-width="120"
          prop="machineName"
        />
        <el-table-column align="center" label="机长" prop="headUserName" />
        <el-table-column align="center" label="生产单编码" prop="produceNo" />
        <el-table-column align="center" label="出版编号" prop="pubNo" />
        <el-table-column align="center" label="品种" prop="breedType" />
        <el-table-column align="center" label="印张" prop="sheet" />
        <el-table-column align="center" label="订单数量" prop="orderCount" />
        <el-table-column align="center" label="用纸令数" prop="usePaperNum" />
      </el-table>
      <pagination
        v-if="isActive == 2 || isActive == 6"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getGoods"
      />
      <!-- 欠货排查分页面 -->
      <!-- <div v-if="isActive == 2">
        <div v-if="urgentDateList" class="u-page">
          <span style="color: #606266; font-size: 14px">
            共 {{ pageTotal }} 条
          </span>
          <div
            style="margin-left: 10px"
            v-for="(item, index) in urgentDateList?.split(',')"
            :key="index"
          >
            <div
              class="page"
              :class="{ 'current-page': index == currentIndex }"
              @click="handleViewPage(item, index)"
            >
              {{ item.split("-")[2] }}
            </div>
          </div>
        </div>
      </div> -->
      <pagination
        v-else-if="isActive == 3"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getWaitPrintInfo"
      />
      <pagination
        v-else-if="isActive == 4"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getBind"
      />

      <pagination
        v-else-if="isActive == 5"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getInvoiceListInfo"
      />

      <pagination
        v-else-if="isActive == 7"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getQuestionScheduleInfo"
      />
      <pagination
        v-else-if="isActive == 8 || isActive == 9"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getTasksApi"
      />

      <pagination
        v-else
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>
  
  <script setup name="OrderStatistics">
import { getMachineTask } from "@/api/production/cover.js";
import { getList as getWorkBenchList } from "@/api/basicInfo/workbench";
import {
  getInvoiceList,
  getWaitPrint,
  getPlannedBindOrPrintTasks,
} from "@/api/dataSummary/orderStatistics.js";
import {
  getNWScheduledetail,
  getShortageGoods,
  getBinding,
  getQuestionSchedule,
} from "@/api/production/orderProgress.js";
import { getMesWorkShop } from "@/api/basicInfo/workshop.js";
import { ref } from "@vue/reactivity";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
const notInPaperNumSum = ref(0);
const usePaperNumSum = ref(0);
const urgentNumSum = ref(0);
const workshopOptions = ref([]);
const flag = ref(false);
const isActive = ref("1");
const currentIndex = ref(0);
const urgentDateList = ref([]);
const pageTotal = ref(0);
const urgentNumTotal = ref(0);
const lingCountSum = ref(0);
const taskList = ref([]);
const workbranchList = ref([]);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `新/旧版`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版编码`, visible: true },
  { key: 4, label: `生产单编码`, visible: true },
  { key: 5, label: `出版编码`, visible: true },
  { key: 6, label: `书刊编码`, visible: true },
  { key: 7, label: `书刊名称`, visible: true },
  { key: 8, label: `印刷序列号`, visible: true },
  { key: 9, label: `品种`, visible: true },
  { key: 10, label: `组成`, visible: true },
  { key: 11, label: `印张`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `印色`, visible: true },
  { key: 14, label: `版材数量`, visible: true },
  { key: 15, label: `制版数量`, visible: true },
  { key: 16, label: `版材尺寸`, visible: true },
  { key: 17, label: `制版人`, visible: true },
  { key: 18, label: `烤版人`, visible: true },
  { key: 19, label: `完成日期`, visible: true },
  { key: 20, label: `取版人`, visible: true },
  { key: 21, label: `取版日期`, visible: true },
  { key: 22, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 100,
  },
});

const { queryParams } = toRefs(data);
const {
  ems_workshop,
  ems_machine_info_size,
  ems_order_binding_mode,
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
} = proxy.useDict(
  "ems_workshop",
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "ems_machine_info_size",
  "ems_order_binding_mode"
);
const getWorkBenchListInfo = (type) => {
  getWorkBenchList({ pageNum: 1, pageSize: 100,status:2, type }).then((resp) => {
    resp.rows.forEach((item) => {
        //item.machineId = item.pid;
        item.machineName = item.machineName + "/" + item.workshopName;
      });
    workbranchList.value = resp.rows;
  });
};

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  queryParams.value.startDate = firstDay;
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  queryParams.value.endDate = currentDay;

  return currentDay;
};

const getCurrentLastDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  var nextMonth = new Date(year, month, 1);
  var lastDayOfMonth = new Date(nextMonth.getTime() - 24 * 60 * 60 * 1000);
  var lastDay = lastDayOfMonth.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (lastDay >= 0 && lastDay <= 9) {
    lastDay = "0" + lastDay;
  }
  const currentDay = year + "-" + month + "-" + lastDay;
  queryParams.value.endDate = currentDay;

  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);

// 生产汇总
const handleQueryOrderSummary = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  queryParams.value.pageNum = 1;
  isActive.value = "1";
  queryParams.value.zbDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;
  getList();
};

//问题订单
const handleQueryQuestionOrder = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  queryParams.value.pageNum = 1;
  isActive.value = "7";
  queryParams.value.zbDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;
  getQuestionScheduleInfo();
};

const getQuestionScheduleInfo = () => {
  loading.value = true;
  getQuestionSchedule(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        urgentNumSum.value = res.rows[0].urgentNumTotal;
      }

      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

const getList = () => {
  loading.value = true;
  getNWScheduledetail(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        urgentNumSum.value = res.rows[0].urgentNumTotal;
      }

      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
  const fields = [
    "status",
    "isSuspend",
    "produceNo",
    "shipmentType",
    "workshopName",
    "urgentDate",
    "submitDate",
    "deliverDate",
    "sheet",
    "orderCount",
    "fhDate",
    "usePaperNum",
    "notInPaperNum",
    "lingCount",
    "urgentNum",
  ];
  const currentProduceNo = row.produceNo;
  const cellValue = row[column.property];
  if (cellValue && fields.includes(column.property)) {
    const prevRow = dataList.value[rowIndex - 1]; //上一行数据
    let nextRow = dataList.value[rowIndex + 1]; //下一行数据
    if (
      prevRow &&
      prevRow[column.property] === cellValue &&
      prevRow.produceNo === currentProduceNo
    ) {
      return { rowspan: 0, colspan: 1 };
    } else {
      let countRowspan = 1;
      while (
        nextRow &&
        nextRow[column.property] === cellValue &&
        nextRow.produceNo === currentProduceNo
      ) {
        nextRow = dataList.value[++countRowspan + rowIndex];
      }
      if (countRowspan > 1) {
        return { rowspan: countRowspan, colspan: 1 };
      }
    }
  }
};

// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }

    switch (column.property) {
      case "notInPaperNum":
        sums[index] = notInPaperNumSum.value;
        break;
      case "usePaperNum":
        sums[index] = usePaperNumSum.value;
        break;
      case "lingCount":
        sums[index] = lingCountSum.value;
        break;
      case "urgentNum":
        sums[index] = isActive == 2 ? urgentNumTotal.value : urgentNumSum.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 合计
const getSummariesTask = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }

    switch (column.property) {
      case "usePaperNum":
        sums[index] = usePaperNumSum.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  flag.value = false;
  queryParams.value.pageNum = 1;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  if (isActive.value == 2 || isActive.value == 6) {
    //欠货统计查询
    getGoods();
  } else if (isActive.value == 3) {
    //1=生产单汇总，3=待印刷
    // getList();
    getWaitPrintInfo();
  } else if (isActive.value == 1) {
    getList();
  } else if (isActive.value == 4) {
    //待装订
    getBind();
  } else if (isActive.value == 5) {
    //已发货
    // queryParams.value.fhDate = "已发货";
    // getList();
    getInvoiceListInfo();
  } else if (isActive.value == 7) {
    getQuestionScheduleInfo();
  } else if (isActive.value == 8 || isActive.value == 9) {
    getTasksApi();
  }
};
const getInvoiceListInfo = () => {
  loading.value = true;
  getInvoiceList(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        urgentNumSum.value = res.rows[0].urgentNumTotal;
        urgentDateList.value = res.rows[0].urgentDateList;
        urgentNumTotal.value = res.rows[0].urgentNumTotal;
        lingCountSum.value = res.rows[0].lingCountSum;
      }
      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

const commonFun = () => {
  dataList.value = [];
  queryParams.value.qhDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.fhDate = undefined;
  proxy.resetForm("queryRef");
};
//印刷已排产
const handlePrintTasks = () => {
  //dateRange.value = [getDateNow(), getCurrentDay()];
  dateRange.value = [];
  isActive.value = "8";
  commonFun();
  queryParams.value.ysDate = "已排产";
  queryParams.value.zdDate = undefined;
  queryParams.value.machineType = 2;
   //默认不传时间
   queryParams.value.startDate = undefined;
  queryParams.value.endDate = undefined;

  getTasksApi();
  getWorkBenchListInfo(2);
};
//装订已排产
const handleBindTasks = () => {
  //dateRange.value = [getDateNow(), getCurrentDay()];
  dateRange.value = [];
  isActive.value = "9";
  getWorkBenchListInfo(3);
  commonFun();
  queryParams.value.machineType = 3;
  queryParams.value.zdDate = "已排产";
  queryParams.value.ysDate = undefined;
  //默认不传时间
  queryParams.value.startDate = undefined;
  queryParams.value.endDate = undefined;
  getTasksApi();
};

const getTasksApi = () => {
  loading.value = true;
  getPlannedBindOrPrintTasks(queryParams.value)
    .then((res) => {
      total.value = res.total;
      taskList.value = res.rows;
      loading.value = false;
      total.value = res.total;
      usePaperNumSum.value = res.rows[0]?.usePaperNumTotal || 0;
    })
    .catch((err) => {
      loading.value = false;
    });
};
/** 重置按钮操作 */
const resetQuery = () => {
  isActive.value = "1";
  // queryParams.value.startDate = undefined;
  queryParams.value.zbDate = undefined;
  queryParams.value.qhDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.zdDate = undefined;
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const getWorkShopList = async () => {
  const res = await getMesWorkShop({ ...queryParams.value });
  workshopOptions.value = [...res.rows];
};
getWorkShopList();

//未履行订单
const handleQueryUnfulfilledOrders = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  queryParams.value.pageNum = 1;
  isActive.value = "6";
  queryParams.value.zbDate = undefined;
  queryParams.value.fhDate = "未履约";
  queryParams.value.ysDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;
  flag.value = true;
  getGoods();
};

// 欠货统计查询
const handleQueryShortageGoods = () => {
  //dateRange.value = [getDateNow(), getCurrentLastDay()];
  dateRange.value = [];
  queryParams.value.pageNum = 1;
  isActive.value = "2";
  queryParams.value.zbDate = undefined;
  queryParams.value.fhDate = "未发货";
  queryParams.value.ysDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;

  //默认不传时间
  queryParams.value.startDate = undefined;
  queryParams.value.endDate = undefined;

  flag.value = true;
  getGoods();
};
const handleViewPage = (item, index) => {
  queryParams.value.qhDate = item;
  currentIndex.value = index;
  getGoods();
};
const getGoods = () => {
  loading.value = true;
  getShortageGoods(queryParams.value)
    .then((res) => {
      total.value = res.total;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        urgentNumSum.value = res.rows[0].urgentNumTotal;
        urgentDateList.value = res.rows[0].urgentDateList;
        urgentNumTotal.value = res.rows[0].urgentNumTotal;
      }

      loading.value = false;
      pageTotal.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
// 待印刷
const handleQueryPrint = () => {
  //dateRange.value = [getDateNow(), getCurrentDay()];
  dateRange.value = [];
  queryParams.value.pageNum = 1;
  isActive.value = "3";
  flag.value = false;
  queryParams.value.fhDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.qhDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;

   //默认不传时间
   queryParams.value.startDate = undefined;
  queryParams.value.endDate = undefined;

  getWaitPrintInfo();
};
const getWaitPrintInfo = () => {
  loading.value = true;
  getWaitPrint(queryParams.value).then((res) => {
    dataList.value = res.rows;
    if (res.rows.length > 0) {
      usePaperNumSum.value = res.rows[0].usePaperNumTotal;
      notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
      urgentNumSum.value = res.rows[0].urgentNumTotal;
    }
    loading.value = false;
    total.value = res.total;
  });
};
// 待装订
const handleQueryBinding = () => {
  dateRange.value = [];
  queryParams.value.pageNum = 1;
  isActive.value = "4";
  flag.value = false;
  queryParams.value.zbDate = undefined;
  queryParams.value.qhDate = undefined;
  queryParams.value.fhDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;

   //默认不传时间
   queryParams.value.startDate = undefined;
  queryParams.value.endDate = undefined;

  getBind();
};

// 已发货
const handleQueryInvoice = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  queryParams.value.pageNum = 1;
  isActive.value = "5";
  flag.value = false;
  queryParams.value.zbDate = undefined;
  queryParams.value.qhDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.zdDate = undefined;
  queryParams.value.ysDate = undefined;
  queryParams.value.fhDate = undefined;
  // queryParams.value.fhDate = "已发货";
  // queryParams.value.startDate = undefined;
  // queryParams.value.endDate = undefined;
  queryParams.value.workshopName = undefined;
  queryParams.value.machineType = undefined;

  getInvoiceListInfo();
};

const getBind = () => {
  loading.value = true;
  getBinding(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        urgentNumSum.value = res.rows[0].urgentNumTotal;
      }

      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

const handleViewCode = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.platemakingId,
      info: true,
    },
  });
};

const handleView = (row, type) => {
  router.push({
    path: "/Productionmanagementdistribution/production/searchReportDetail",
    query: {
      breed: row.breedType,
      type: type,
      produceNo: row.produceNo,
      status: -2,
      info: true,
    },
  });
};

// 折页中 印刷中 覆膜中 裁切中
const handleViewLoading = (row, type) => {
  router.push({
    path: "/production/orderProgress/processPenetration",
    query: {
      breedType: row.breedType,
      machineType: type,
      produceNo: row.produceNo,
      info: true,
    },
  });
};

//发货数量
const handleViewCount = (row, type) => {
  router.push({
    path: "/production/orderProgress/quantityPenetration",
    query: {
      produceNo: row.produceNo,
      info: true,
    },
  });
};

// getList();
handleQueryOrderSummary();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>
  
  <style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
.page {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 32px;
  height: 32px;
  background-color: #f0f2f5;
  margin: 0 4px;
  color: black;
  // font-weight: 700;
}
.u-page {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 50px;
  flex-wrap: wrap;
}

.current-page {
  background-color: #5a9cf8;
  color: #fff;
}
</style>
  